//
//  FileListViewController.m
//  uplife
//
//  Created by ZhangCheng on 2018/12/26.
//  Copyright © 2018年 ZhangCheng. All rights reserved.
//

#import "FileListViewController.h"
#import "WebViewController.h"
@interface FileListViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,weak)IBOutlet UICollectionView *collectionView;
@end

@implementation FileListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = [NSString stringWithFormat:@"%@",self.dataDic[@"typeName"]];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSArray *array = self.dataDic[@"files"];
    return array.count;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = ([UIScreen mainScreen].bounds.size.width - 40.0f)/3.0f-1.0f;
    CGFloat height = 110.0f / 75.0f * width;
    return CGSizeMake(width, height);
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSArray *array = self.dataDic[@"files"];
    NSDictionary *dic = array[indexPath.row];
    UILabel *titleLabel = [cell viewWithTag:2];
    titleLabel.text = [NSString stringWithFormat:@"%@",dic[@"fileName"]];
    
    UIImageView *imageView = [cell viewWithTag:1];
    NSString *lastString = [titleLabel.text substringFromIndex:titleLabel.text.length-3];
    imageView.image = [UIImage imageNamed:([lastString isEqualToString:@"pdf"]?@"filetype_PDF":@"filetype_Word")];
    return cell;
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:@"toWeb"]) {
        WebViewController *vc = segue.destinationViewController;
        UICollectionViewCell *cell = sender;
        NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
        NSArray *array = self.dataDic[@"files"];
        NSDictionary *dic = array[indexPath.row];
        vc.requestURLString = [NSString stringWithFormat:@"%@/%@",JSXF_HOST_FILE,dic[@"filePath"]];
    }
}


@end
